package com.example.teaknow.ui.collection;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.teaknow.Adapter.itemAdapter;
import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Dao.CollectionDao;
import com.example.teaknow.Dao.NewsDao;
import com.example.teaknow.NewsDetailActivity;
import com.example.teaknow.R;

import java.util.ArrayList;

public class CollectionFragment extends Fragment {
    private ArrayList<NewsBean> newsBeans=new ArrayList<NewsBean>();
    private ListView listView;
    private CollectionDao collectionDao;
    private Boolean flag=false;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_collection, container, false);
        listView=(ListView)root.findViewById(R.id.lv_load_fav);
        initView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                Intent intent=new Intent(getContext(), NewsDetailActivity.class);
                NewsBean newsBean=collectionDao.queryCollection().get(i);
                newsBean.setFlag(1);
                intent.putExtra("newsBean",newsBean);
                startActivity(intent);
            }
        });

        return root;
    }

    private void initView() {
        if (flag){

        }
        else {
            flag=true;
            collectionDao=new CollectionDao(getActivity());
            newsBeans=collectionDao.queryCollection();
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listView.setAdapter(new itemAdapter(newsBeans,getContext()));
            }
        });
    }
}